//
//  KDAccount.m
//  警用舆情平台
//
//  Created by 魏强 on 15/5/12.
//  Copyright (c) 2015年 魏强. All rights reserved.
//

#import "KDAccount.h"
#import "MJExtension.h"
@implementation KDAccount
/**
 *  当一个对象要归档进沙盒中时，就会调用这个方法
 *  目的：在这个方法中说明这个对象的哪些属性要存进沙盒
 */
- (void)encodeWithCoder:(NSCoder *)encoder{
    unsigned int count = 0;
    Ivar *ivars = class_copyIvarList([KDAccount class], &count);
    for (int i = 0; i < count; i ++) {
        Ivar ivar = ivars[i];
        //取出i位置de成员变量
        const char *name = ivar_getName(ivar);
        
        //
        NSString *key = [NSString stringWithUTF8String:name];
        id value = [self valueForKey:key];
        [encoder encodeObject:value forKey:key];
    }
    free(ivars);
}

/**
 *  当从沙盒中解档一个对象时（从沙盒中加载一个对象时），就会调用这个方法
 *  目的：在这个方法中说明沙盒中的属性该怎么解析（需要取出哪些属性）
 */
- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init]) {
        unsigned int count = 0;
        Ivar *ivars = class_copyIvarList([KDAccount class], &count);
        for (int i = 0; i < count; i ++) {
            Ivar ivar = ivars[i];
            //取出i位置de成员变量
            const char *name = ivar_getName(ivar);
            
            //
            NSString *key = [NSString stringWithUTF8String:name];                id value = [aDecoder decodeObjectForKey:key];
                if (value) {
                    [self setValue:value forKey:key];
                }
        }
         free(ivars);
    }
    return self;
}
- (NSString *)description
{
    [super description];
    return [NSString stringWithFormat:@"token = %@,username = %@, user_Type = %ld", self.token, self.userName,(long)self.user_Type];
}
- (NSString *)tokens
{
//    NSString* webStringURL = [self.token stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

    NSString *token = [self.token stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//   NSString * token = [webStringURL stringByReplacingOccurrencesOfString:@"+" withString:@"%2B" options:NSLiteralSearch range:NSMakeRange(0, webStringURL.length)];

    KDLog(@"%@\n%@",self.token,token);
    return token;
}
@end
